//
//  CloneController.m
//  iLiteNote
//
//  Created by xiangbc on 16/4/10.
//  Copyright © 2016年 LiteNice. All rights reserved.
//

#import "CloneController.h"
#import "GitInfo.h"
#import "GitCmd.h"

@interface CloneController () {
    UITextField *_txtURL;
    UITextField *_txtAccount;
    UITextField *_txtPassword;
}

@end

@implementation CloneController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [self createWidget];
}

- (void)createWidget {
    UINavigationItem *navItem = self.navigationItem;
    navItem.title = @"设置";
    
    CGRect frame = self.view.frame;
    int y = 100;
    int h = 50;
    CGColorRef c = [[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0] CGColor];
    
    UITextField *txtSite = [[UITextField alloc] initWithFrame:CGRectMake(0, y, frame.size.width, h)];
    [txtSite.layer setBorderColor:c];
    [txtSite.layer setBorderWidth:1];
    txtSite.placeholder = @"git地址";
    txtSite.backgroundColor = [UIColor whiteColor];
    [txtSite setText:@"https://git.oschina.net/xiangism/txtNote.git"];
    _txtURL = txtSite;
    
    y += h-1;
    
    UITextField *txtAccount = [[UITextField alloc] initWithFrame:CGRectMake(0, y, frame.size.width, h)];
    [txtAccount.layer setBorderColor:c];
    [txtAccount.layer setBorderWidth:1];
    txtAccount.placeholder = @"git账号";
    txtAccount.backgroundColor = [UIColor whiteColor];
    _txtAccount = txtAccount;
    
    y += h-1;
    
    UITextField *txtPass = [[UITextField alloc] initWithFrame:CGRectMake(0, y, frame.size.width, h)];
    [txtPass.layer setBorderColor:c];
    [txtPass.layer setBorderWidth:1];
    txtPass.placeholder = @"git密码";
    txtPass.backgroundColor = [UIColor whiteColor];
    [txtPass setSecureTextEntry:YES];
    _txtPassword = txtPass;
    
    y += h+20;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(100, y, 50, h);
    [btn setTitle:@"Clone" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClone:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:txtSite];
    [self.view addSubview:txtAccount];
    [self.view addSubview:txtPass];
    [self.view addSubview:btn];
}

- (void)btnClone:(UIButton*)btn {
    GitInfo *info = [GitInfo shareGitInfo];
    info.gitURL = _txtURL.text;
    info.account = _txtAccount.text;
    info.password = _txtPassword.text;
    
    NSRange r1 = [info.gitURL rangeOfString:@"/" options:NSBackwardsSearch];
    NSRange r2 = [info.gitURL rangeOfString:@"." options:NSBackwardsSearch];
    
    info.locatePath = [info.gitURL substringWithRange:NSMakeRange(r1.location+1, r2.location-r1.location-1)];
    [info WriteGitInfo];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *localPath = [info getGitPath];
    BOOL isDir;
    
    // 如果已经存在同名的文件夹，则先将同名的文件夹改名
    if ([fileManager fileExistsAtPath:localPath isDirectory:&isDir]) {
        
        NSString *newName;
        
        for (int i = 1; ;++i) {
            newName = [NSString stringWithFormat:@"%@_temp%d", localPath, i];
            if (![fileManager fileExistsAtPath:newName isDirectory:&isDir]) {
                break; 
            }
        }
        
        NSURL *url = [NSURL fileURLWithPath:localPath];
        NSURL *url2 = [NSURL fileURLWithPath:newName];
        [fileManager moveItemAtURL:url toURL:url2 error:nil];
    }
    
    GitControl *git = [[GitControl alloc] initWithInfo:info];
    int r = [git gitClone];
    printf("Clone: %d\n", r);
    
    if (self.treeViewReloadBlock)
        self.treeViewReloadBlock();
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
